let express = require('express')
let query = require('../query/query')
// import query from '../query/query'
// express.query
let router = express.Router()


router.use(express.json());
router.use(express.urlencoded({
    extended:false
}))


// 获取地址表中所有内容
router.get('/getList',(req,res)=>{
    let sql = 'select * from address'
    query(sql).then(response=>{
        res.send(response)
    })
})

router.post('/add',(req,res)=>{
    let {name,phone,addr,address,isdafault} = req.body
    console.log(isdafault);
    
    let sql = 'insert into address(name,phone,addr,address,isdafault) values(?,?,?,?,?)'
    query(sql,[name,phone,addr,address,isdafault]).then(response=>{
        res.send(response)
    })
})




// 删除
router.get('/del',(req,res)=>{
    let {id} = req.query
    
    let sql = 'delete from address where id =' +id;
    query(sql).then(response=>{
        res.send(response)
    })
})


// 根据id获取要修改的数据，进行反显
router.get('/show',(req,res)=>{
    let {id} = req.query
    
    let sql = 'select * from address where id =' +id;
    query(sql).then(response=>{
        res.send(response)
    })
})

// 根据id修改数据

router.post('/update',(req,res)=>{
    let {name,phone,addr,address,id} = req.body
    let sql = 'update address  set name=?, phone=?,addr=?,address=? where id=?';
    query(sql,[name,phone,addr,address,id]).then(response=>{
        res.send(response)
    })
})





module.exports =router